/*
 * Author: Jeff Liu(jebberwocky@gmail.com)
 * Date:   Sep 4, 2010 9:11:27 PM
 * 
 * License: MIT
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package org.gglgeo.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;

import org.gglgeo.core.Const;


/**
 * The Class WebUtil.
 * For sending web request
 * @since 0.0.1
 */
public class WebUtil {
	 
 	/**
 	 * Gets the request & return the responce text in json, xml or...
 	 *
 	 * @param url the url
 	 * @return the request
 	 */
 	public static String getRequest(String url) {
		try {
			URL _url = new URL(url);
			URLConnection connection = _url.openConnection();
	        connection.connect();
	        
	        BufferedReader rd  = new BufferedReader(
	        		new InputStreamReader(connection.getInputStream(),Const.CHARSET));
	        StringBuilder sb = new StringBuilder();
	        String line = null;
	        while ((line = rd.readLine()) != null){
	              sb.append(line + '\n');
	        }
	        return sb.toString();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	 }
}
